package movies.repositoryHibernate;

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import movies.domain.User;
import movies.repository.IUserRepository;

/**
 *
 * @author Hanna_Aliakseichykava
 */
public class UserRepository extends HibernateDaoSupport
        implements IUserRepository{

    @Override
     public User registration(User user){

         Integer id = (Integer)getHibernateTemplate().save(user);
         user.setId(id);
         return user;

     }

    @Override
    public User getUserByLogin(final String login) {
        List < User > users = getHibernateTemplate().
             findByNamedQuery("GetUserByLogin", login);

        if (users.size() == 1) {
            return users.get(0);
        } else {
            return null;
        }
    }

    @Override
    public User getUserById(final int id){
        List < User > users = getHibernateTemplate().
             findByNamedQuery("GetUserById", id);

        if (users.size() == 1) {
            return users.get(0);
        } else {
            return null;
        }
    }

    @Override
     public void deleteUser(User user){
        getHibernateTemplate().delete(user);
    }

    @Override
    public void updateUser(User user) {
        getHibernateTemplate().update(user);
    }

}
